package com.you.weixinpay.config;

import com.alipay.api.*;
import com.you.weixinpay.properties.AliPayProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class AliPayConfig {
    private final AliPayProperties aliPayProperties;
    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        log.info("【支付宝】参数开始初始化......");
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置应用Id
        alipayConfig.setAppId(aliPayProperties.getAppid());
        //设置网关地址
        alipayConfig.setServerUrl(aliPayProperties.getGatewayUrl());
        //设置应用私钥
        alipayConfig.setPrivateKey(aliPayProperties.getMerchantPrivateKey());
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        //设置字符集
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(aliPayProperties.getAlipayPublicKey());
        //设置签名类型
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        return alipayClient;
    }
}
